package com.alen.sun.controller;

import com.alen.sun.bean.CheckIn;
import com.alen.sun.bean.Folk;
import com.alen.sun.bean.PageBean;
import com.alen.sun.bean.Result;
import com.alen.sun.dao.FolkMapper;
import com.alen.sun.service.FolkService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@CrossOrigin
@RequestMapping("/folks")
@RestController
public class FolkController {
    @Autowired
    private FolkService folkService;
    @Autowired
    private FolkMapper folkMapper;

//    @GetMapping()
//    @ResponseBody
//    public Result login() {
//        Folk folk = new Folk("1", "1", "1", "1", 1, "1");
//        log.info("123");
//        List<Folk> folks=new ArrayList<>();
//        folks.add(folk);
//
//        return Result.success(folks);
//    }

//    /**
//     * 注册家属账号
//     * @param folk
//     * @return
//     */
//    @PostMapping()
//    @ResponseBody
//    public Result register(@RequestBody Folk folk) {
//        log.info("添加folk，{}",folk.getName());
//        folkService.addFolk(folk);
//        return Result.success();
//    }

    /**
     * 添加家属账号
     * @param folk
     * @return
     */
    @PostMapping()
    @ResponseBody
    public Result addFolk(@RequestBody Folk folk) {
        Boolean flag = folkService.addFolk(folk);
        if(flag) {
            //true--注册成功
            return Result.success();
        }else {
            //false--注册失败
            return Result.error("该手机号已注册");
        }

    }

    /**
     * 发送验证码
     * @return
     */
    @PostMapping("/captcha")
    @ResponseBody
    public Result sendCode(@RequestBody Map<String, String> requestData) {
        String phoneNum = requestData.get("phoneNum");
        Boolean flag = folkService.sendSms(phoneNum);
        if(flag) {
            return Result.success();
        }else {
            return Result.error("验证码发送失败");
        }

    }

    /**
     * 注册家属账号
     * @return
     */
    @PostMapping("/register")
    @ResponseBody
    public Result register(@RequestBody Map<String, Object> jsonData){
        Folk folk = new ObjectMapper().convertValue(jsonData.get("folk"), Folk.class);
        String captcha = (String) jsonData.get("captcha");
        Boolean flag = folkService.registerFolk(folk, captcha);
        if(flag){
            return Result.success();
        }else {
            return Result.error("注册失败");
        }
    }

    /**
     * 登录家属账号
     */
    @PostMapping("login")
    @ResponseBody
    public Result login(@RequestBody Folk folk) {
        log.info("123456");
        log.info("{}",folk);
        Boolean flag = folkService.login(folk);
        if(flag){
            return Result.success();
        }else {
            Folk judge = folkMapper.selectPhoneNum(folk);
            if(judge != null){
                return Result.error("账号或密码错误");
            }else {
                return Result.error("该用户不存在");
            }
        }
    }

    /**
     * 修改家属个人信息
     * @param folk
     * @return
     */
    @PutMapping
    @ResponseBody
    public Result updateFolk(@RequestBody Folk folk) {
        Boolean flag = folkService.updateFolk(folk);
        if(flag){
            //更新成功
            return Result.success();
        }else {
            //更新失败
            return Result.error("信息更新失败");
        }
    }


    /**
     * 删除家属
     * @param folk
     * @return
     */
    @DeleteMapping
    @ResponseBody
    public Result deleteFolk(@RequestBody Folk folk) {
        Boolean flag = folkService.deleteFolk(folk);
        if(flag){
            return Result.success();
        }else {
            return Result.error("删除家属账号失败");
        }


    }

    /**
     * 分页查询
     * @param requestParams
     * @return
     */
    @PostMapping("/selectByPage")
    @ResponseBody
    public Result page(@RequestBody Map<String, Integer> requestParams) {
        Integer page = requestParams.getOrDefault("page", 1);
        Integer pageSize = requestParams.getOrDefault("pageSize", 10);
        PageBean pageBean = folkService.page(page, pageSize);
        return Result.success(pageBean);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/ids")
    @ResponseBody
    public Result deleteByIds(@RequestBody List<String> ids) {
        Boolean flag = folkService.deleteByIds(ids);
        if(flag){
            return Result.success();
        }else {
            return Result.error("批量删除失败");
        }
    }

    //查找家属账号

    /**
     * 获取家属个人信息
     * @param requestParams
     * @return
     */
    @PostMapping("/getPersonalFolkInfo")
    @ResponseBody
    public Result getPersonalFolkInfo(@RequestBody Map<String, String> requestParams){
        String phoneNum = requestParams.get("phoneNum");
        Folk folk = folkService.getPersonalFolkInfo(phoneNum);
        if(folk.getPhoneNum()==null && folk.getName()==null){
            return Result.error("未查询该账号");
        }
        return Result.success(folk);
    }

    /**
     * 网页搜索功能 聚合模糊查询, 姓名,老人电话号码,家属电话号码
     * @param JsonData
     * @return
     */
    @PostMapping("/searchByKeywords")
    @ResponseBody
    public Result searchByKeywords(@RequestBody Map<String,String> JsonData){
        String keywords = JsonData.get("keywords");
        List<Folk> folks = folkService.searchByKeywords(keywords);
        if(!folks.isEmpty()){
            return Result.success(folks);
        }else {
            return Result.error("未查询到相关结果");
        }
    }

}
